//
//  LeftViewController.m
//  QuickCure
//
//  Created by 马文星 on 16/4/28.
//  Copyright © 2016年 Demos. All rights reserved.
//

#import "LeftViewController.h"
#import "RegisterViewController.h"

@interface LeftViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *tableviewRightConstraint;
@property (weak, nonatomic) IBOutlet UIView *bottom1;
@property (weak, nonatomic) IBOutlet UIView *bottom2; //未登录显示

/** sb上的控件 */
@property (weak, nonatomic) IBOutlet UIImageView *headImageView;
@property (weak, nonatomic) IBOutlet UILabel *nickLabel;
@property (weak, nonatomic) IBOutlet UILabel *desLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableVIew;

/** 数组数据 */
@property (nonatomic, strong) NSArray *titleNameArray1;
@property (nonatomic, strong) NSArray *titleNameArray2;


@property (nonatomic, assign) BOOL isLogin; //是否登录

@end

@implementation LeftViewController

-(void)awakeFromNib{


}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableVIew.delegate = self;
    self.tableVIew.dataSource = self;
    
    [self setUI];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    [self setUpHide];
  
}

#pragma mark  - 公共方法
-(void)setUpHide{

    NSUserDefaults *defaults = (NSUserDefaults *)[NSUserDefaults standardUserDefaults];
    
    BOOL isLogin =  [defaults boolForKey:kIsUserLoginKey];
    
    self.bottom1.hidden = !isLogin;
    self.bottom2.hidden = isLogin;
    
    if (isLogin) {
        
        [[UserDTO sharedInstance] loadUserInfoFromSanbox];
        
        
        UserDTO *dto = [UserDTO sharedInstance];
        
        self.nickLabel.text = dto.true_name;
        self.desLabel.text = dto.card_number;
        
    }
    [self.tableVIew reloadData];
}

-(void)setUI{
    
    self.bottom1.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"nav"]];
    
//    self.navigationController.navigationBar.hidden = YES;
    
    self.titleNameArray1 = @[@"名医通申请",@"病例管理",@"关注医生"];
    self.titleNameArray2 = @[@"设置与帮助",@"关于产品",@"分享给好友"];
    
    
    self.tableviewRightConstraint.constant = SCREEN_WIDTH*0.2;
    
    [self.headImageView cornerRadius:30];
    
}


#pragma mark  - UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    NSUserDefaults *defaults = (NSUserDefaults *)[NSUserDefaults standardUserDefaults];
    BOOL isLogin =  [defaults boolForKey:kIsUserLoginKey];
    
    if (!isLogin) {
        
        return 0;
    }else{
    
        return 2;
    }
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 3;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    
    if (indexPath.section==0) {
        
        cell.textLabel.text = self.titleNameArray1[indexPath.row];
        
    }else{
        
        cell.textLabel.text = self.titleNameArray2[indexPath.row];
        
    }
    
    
    cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    
    return cell;
}

#pragma mark  - UITableViewDelegate
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (indexPath.section==0) {
        
        //名医通申请
        if (indexPath.row == 0) {
            
         [self jump2SunVCWithSBName:@"Famous" identifier:@"FamousDoctorViewController"];
            
        }

        //病例管理
        else if (indexPath.row == 1) {
            
         [self jump2SunVCWithSBName:@"CaseManageViewController" identifier:@"CaseManageViewController"];
        }
        //关注医生
        else{
            
          [self jump2SunVCWithSBName:@"FocusDoctorViewController" identifier:@"FocusDoctorViewController"];
        }
        
    }else if(indexPath.section==1){
        
        //设置与帮助
        if (indexPath.row == 0) {
           SettingAndHelpViewController *newVc =  ( SettingAndHelpViewController *)[self jump2SunVCWithSBName:@"SettingAndHelpViewController" identifier:@"SettingAndHelpViewController"];
            
            WEAKSELF
            newVc.loginOutBlock = ^{
                
                loggerWarn(@"退出登录");
                
                [weakSelf setUpHide];
                
            };
        }
        //关于产品
        else if (indexPath.row == 1) {
          [self jump2SunVCWithSBName:@"AboutProductViewController" identifier:@"AboutProductViewController"];
        }
        //分享给好友
        else{
         [self jump2SunVCWithSBName:@"Shared2FriendViewController" identifier:@"Shared2FriendViewController"];
        }
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

//分别跳转到指定的VC中去
-(UIViewController *)jump2SunVCWithSBName:(NSString *)sbName identifier:(NSString *)identifier{
    
    //显示中间
    [self.sidePanelController showCenterPanelAnimated:YES];
    
    UIStoryboard *sb= [UIStoryboard storyboardWithName:sbName bundle:nil];
    UIViewController *vc = [sb instantiateViewControllerWithIdentifier:identifier];
    
    //push到下一个VC
    [[self centerNavigationController] pushViewController:vc animated:NO];
    
    return vc;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    
    if (section==0) {
        
        return 0.01;
    }else{
        
        return 10;
    }
}

#pragma mark  - Actions
- (IBAction)clickRegisterButton:(id)sender {
    
    
  RegisterViewController *registerVC =  (RegisterViewController *)[UIViewController creatVCByStoryBoardWIthSBName:@"Register" VCIdentify:@"RegisterViewController"];
    
    WEAKSELF
    registerVC.loginBlock = ^{
        
        loggerWarn(@"登录了");
        
        [weakSelf setUpHide];
        
    };
    
    [self jump2OtherViewControllerByMenu:registerVC];
    
    loggerImpt(@"点击注册  left");
    
    
    
}

- (IBAction)clickLoginButton:(id)sender {
    
    LoginViewController *loginVC =  (LoginViewController *)[UIViewController creatVCByStoryBoardWIthSBName:@"Login" VCIdentify:@"LoginViewController"];
    
    WEAKSELF
    loginVC.loginBlock = ^{
    
        loggerWarn(@"登录了");
        
        [weakSelf setUpHide];
            
    };
    
    [self jump2OtherViewControllerByMenu:loginVC];

}

#pragma mark  - Lazy



@end
